package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.umeng.analytics.MobclickAgent;

public class OrderWebActivity extends BaseActivity {
    private WebView order_webView;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_web);
        initView();
    }

    private void initView() {
        String itemid = getIntent().getStringExtra("itemid");
        Log.d("yuhao", "itemid=" + itemid);
        int goodsid = getIntent().getIntExtra("goodsid", 0);
        order_webView = ((WebView) findViewById(R.id.order_webView));
        order_webView.setDrawingCacheEnabled(true);
        order_webView.getSettings().setDefaultTextEncodingName("utf-8");
        order_webView.getSettings().setJavaScriptEnabled(true);
        order_webView.addJavascriptInterface(new WebAppInterface(), "Android");
//        String tarUrl="http://wx.zhuiyinanian.com/shop/shop_buy?userid=24326&itemid=1"
        //http://wx.zhuiyinanian.com/shop/shop_buy?userid=24326&itemid=1&orderAudio=test.mp3
        String tarUrl = "http://wx.zhuiyinanian.com/shop/shop_buy?userid=";
        String micurl = getIntent().getStringExtra("micurl");
        Log.d("yuhao", "micurl=" + micurl);
        Log.d("yuhao", "goodsid=" + goodsid);
        if (micurl != null) {
            order_webView.loadUrl(tarUrl + application.getCurID() + "&itemid=" + itemid + "&goodsid=" + goodsid + "&orderAudio=" + micurl);
            Log.d("yuhao", tarUrl + application.getCurID() + "&itemid=" + itemid + "&goodsid=" + goodsid + "&orderAudio=" + micurl);
        } else {
            order_webView.loadUrl(tarUrl + application.getCurID() + "&itemid=" + itemid + "&goodsid=" + goodsid + "&orderAudio=");
            Log.d("yuhao", "goodsid=" + goodsid);
        }
        order_webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });


        mProgressDialog = ProgressDialog.show(this, "", CommonConstants.TIP_LOAD_DATA);
        order_webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    mProgressDialog.dismiss();
                } else {
                    // 加载中
                    mProgressDialog.show();
                }
            }
        });
    }


    public class WebAppInterface {

//        @JavascriptInterface
//        public void doJieTu() {
//            saveBitmap(captureWebViewVisibleSize());
//            SnackBarUtils.makeLong(mToolbar, "截图已保存到本地").show();
//        }

        @JavascriptInterface
        public void doFinish() {
            finish();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && order_webView.canGoBack()) {
            order_webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
